package com.example.composemodifiterdemo

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.requiredHeight
import androidx.compose.foundation.layout.width
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

/**
 *@author: njb
 *@date:  2023/2/7 22:22
 *@desc:
 */
class CircleProgressActivity:ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ProgressSample()
           // SliderSample()
            //SwitchSample()
        }
    }

    @Preview
    @Composable
    fun ProgressSample(){
        Column {
            Spacer(modifier = Modifier.width(20.dp))
            var progress by remember { mutableStateOf(0.1f) }

            val animationProcess by animateFloatAsState(targetValue = progress,
                animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec)
            Column() {
                CircularProgressIndicator(progress = animationProcess)
                Spacer(modifier = Modifier.requiredHeight(30.dp))
                OutlinedButton(onClick = {
                    if(progress <1f) progress += 0.1f
                }) {
                    Text(text = "增加进度")
                }
            }
            //进度条组件
            var slidePosition by remember {
                mutableStateOf(0f)
            }
            Spacer(modifier = Modifier.width(20.dp))
            Text(text = "%.1f".format(slidePosition * 100) + "%")
            Slider(
                value = slidePosition,
                onValueChange = {slidePosition = it })
            val checkedState = remember {
                mutableStateOf(true)
            }
            //switch切换组件
            Switch(
                checked = checkedState.value,
                onCheckedChange = {checkedState.value = it})
        }
    }

    @Composable
    fun SliderSample(){
        Column() {
            //进度条组件
            var slidePosition by remember {
                mutableStateOf(0f)
            }
            Text(text = "%.1f".format(slidePosition * 100) + "%")
            Slider(
                value = slidePosition,
                onValueChange = {slidePosition = it })
        }

    }

    @Composable
    fun SwitchSample(){
        Column() {
            val checkedState = remember {
                mutableStateOf(true)
            }
            //switch切换组件
            Switch(
                checked = checkedState.value,
                onCheckedChange = {checkedState.value = it})
        }
    }
}